﻿<?php
 
defined('_JEXEC') or die('Restricted access'); 
?>

<?	
	$task = $_GET["task"];		
	if(isset($this->event))
		$event = $this->event;
	//var_dump($event)
?>
<script type="text/javascript">
$(document).ready(function(){
$('#start').datetimepicker({
    onClose: function(dateText, inst) {
        var endDateTextBox = $('#end');
        if (endDateTextBox.val() != '') {
            var testStartDate = new Date(dateText);
            var testEndDate = new Date(endDateTextBox.val());
            if (testStartDate > testEndDate)
                endDateTextBox.val(dateText);
        }
        else {
            endDateTextBox.val(dateText);
        }
    },

    onSelect: function (selectedDateTime){
        var start = $(this).datetimepicker('getDate');
        $('#end').datetimepicker('option', 'minDate', new Date(start.getTime()));
    }
});

$('#end').datetimepicker({
    onClose: function(dateText, inst) {
        var startDateTextBox = $('#start');
        if (startDateTextBox.val() != '') {
            var testStartDate = new Date(startDateTextBox.val());
            var testEndDate = new Date(dateText);
            if (testStartDate > testEndDate)
                startDateTextBox.val(dateText);
        }
        else {
            startDateTextBox.val(dateText);
        }
    },
    onSelect: function (selectedDateTime){
        var end = $(this).datetimepicker('getDate');
        $('#start').datetimepicker('option', 'maxDate', new Date(end.getTime()) );
    }
});
});
</script>
<div class="wrapper wmain">
<div id="content">
	<div id="nav">
		<h2>Add New Event</h2>
        <ul>
			<li><a href="#product-add-general">Event general</a></li>
        </ul>
	</div>
<div id="manager">
<form id="adminForm" name="adminForm" action="index.php?option=com_retailshop&view=myshop" method="post" enctype="multipart/form-data">

            <div id="toolbar-box">
              <div class="toolbar">
                <ul>
                  <li><a id="toolbar-save-event" name="btnsave">Save</a></li>
                  <li id="toolbar-cancel"><a href="index.php?option=com_retailshop&view=myshop&task=setting&task=setting">Cancel</a></li>
                </ul>
              </div>
              <div class="title">
                <h2>Events Manager: <?php echo $task =='editevnet' ? 'Edit' : 'Add New'; ?> Event</h2>
              </div>
            </div> 
			<div class="element">				
				<span id="err-event">					
				</span>
			</div>			
       	    	<div class="element">
                  <label>Title</label>
					<input type="text" name="txtTitle" value="<?php echo $task=='editevnet' ? $event->title : ''; ?>">
              	</div>
                <div class="element">
                  <label>Content</label>
                  <textarea type="text" name="txtContent"><?php echo $task=='editevnet' ? $event->content : ''; ?></textarea>
              	</div>
              	<div class="element">
                  <label>Time</label>
                    <input type="text" id="start" class="time" name="txtStartEvent" value="<?php echo $task=='editevnet' ? $event->start_date : ''; ?>"> to
                    <input type="text" id="end" class="time" name="txtEndEvent" value="<?php echo $task=='editevnet' ? $event->end_date : ''; ?>">
              	</div>
          </div>
	<input type="hidden" name="view" value="myshop" />
	<input type="hidden" name="task" value="saveevent" />
	<input type="hidden" name="action" value="<?php echo $task=='editevnet' ? 'edit' : 'addnew'?>" />
	<?php echo $task=='editevnet' ? '<input type="hidden" name="eventid" value="'.$event->id.'" />' : ''?>
</form>
</div>
</div>
</div>
<script>
$('#toolbar-save-event').click(function(){
		var txtTitle = $('input:text[name="txtTitle"]').val();		
		var txtContent = $('textarea[name="txtContent"]').val();		
		var txtStartEvent = $('input:text[name="txtStartEvent"]').val();		
		var txtEndEvent = $('input:text[name="txtEndEvent"]').val();		
		var err = '';
		if(txtTitle=='')
			err +='<p>please enter title</p>';
		if(txtContent=='')
			err +='<p>please enter content</p>';
		if(txtStartEvent=='')
			err +='<p>please choose start date</p>';
		if(txtEndEvent=='')
			err +='<p>please choose end date</p>';
		
		if(err.length != 0){		
			$('#err-event').html(err);
			return false;
		}else{
			$('#err-event').html('');
		}
		document.adminForm.submit();
		return false;
	});
</script>	